import { adminUserIds, defaultListRequestParams } from '@/config';
import api from '@/services/organization/role-management';

export type Role = RoleManagementApi.InfoResponse;
export type RoleList = RoleManagementApi.ListResponse;
export const getList = async (params: Requestor.ListRequestParams) => {
  const res = await api.getList<RoleList>(params);
  const tableData: Requestor.TableFormatedData<RoleList> = {
    data: res.listData,
    current: res.page + 1,
    pageSize: res.limit,
    total: res.count,
    success: res.success,
  };

  return tableData;
};
export const getAllList = async () => {
  const requestParams: Requestor.ListRequestParams = {
    ...defaultListRequestParams,
    page: 0,
    limit: 500,
  };
  const res = await api.getList<Role>(requestParams);
  return res.listData;
};
export const getAllSelectList = async (params: RoleManagementApi.SelectParams) => {
  const res = await api.getAllSelectList<Role[]>(params);
  return res.data;
};
/** 根据用户ID 判断请求列表接口 */
export const getSelectList = async (
  params: RoleManagementApi.SelectParams,
  userId: string = '',
) => {
  return adminUserIds.includes(userId) ? getAllList() : getAllSelectList(params);
};
export const getInfo = async (params: RoleManagementApi.InfoParams) => {
  const res = await api.getInfo<Role>(params);
  return res.data;
};
export const create = async (params: RoleManagementApi.CreateParams) => {
  const data = await api.create(params);
  return data;
};
export const update = async (params: RoleManagementApi.UpdateParams) => {
  const data = await api.update(params);
  return data;
};
export const remove = async (params: RoleManagementApi.DeleteParams) => {
  const data = await api.remove(params);
  return data;
};

export default {
  getList,
  getAllList,
  getAllSelectList,
  getSelectList,
  getInfo,
  create,
  update,
  remove,
};
